package util;

import android.os.Environment;
import android.util.Log;

import java.io.File;

/**
 * Created by ares on 6/20/16.
 */
public class FileUtil {
    public static final int IMAGE_PATH = 1;
    public static final int PLATE_PATH = 2;
    public static final int MODEL_PATH = 3;
    public static final int FILE_PATH = 4;
    public static final int HOME_PATH = 5;

    /**
     * Create a File for saving an image or video
     */
    public static File getOutputFile(String name, int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.
        File mediaStorageDir = null;
        switch (type) {
            case IMAGE_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/image");
                break;
            }
            case PLATE_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/Plate");
                break;
            }
            case MODEL_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/model");
                break;
            }
            case FILE_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/file");
                break;
            }
            case HOME_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019");
                break;
            }
            default:
                return null;
        }
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()) {
            if (! mediaStorageDir.mkdirs()) {
                Log.d("FileUtil", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        //String timeStamp = DateUtil.getDateFormatString(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + name);

        return mediaFile;
    }

    public static String getFilePath(String name, int type) {
        File mediaStorageDir = null;
        File mediaFile;
        switch (type) {
            case PLATE_PATH:
            case IMAGE_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/image");
                mediaFile = new File(mediaStorageDir.getPath() + File.separator + name);
                break;
            }
            case MODEL_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/model");
                mediaFile = new File(mediaStorageDir.getPath() + File.separator + name);
                break;
            }
            case FILE_PATH: {
                mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "Car2019/file");
                mediaFile = new File(mediaStorageDir.getPath() + File.separator + name);
                break;
            }
            case HOME_PATH: {
                mediaFile = new File(Environment.getExternalStorageDirectory(), "Car2019" + name);
                break;
            }
            default:
                return null;
        }
        if(mediaFile.exists())
            return mediaFile.getPath();
        else
            return null;
    }
}
